package com.qf.shiro2203.userr.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.shiro2203.userr.dao.UserDao;
import com.qf.shiro2203.userr.entity.User;
import com.qf.shiro2203.userr.service.IUserService;
import com.qf.shiro2203.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wawawa
 * @since 2023-02-09
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements IUserService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void regis(User user) {
        //注册时 先 生成随机盐字符串
        String salt = SaltUtils.getSalt(8);
        //设置salt到user对象
        user.setSalt(salt);
        //生成加密的密码
        String password = user.getPassword();
        //创建shiro提供的 加密对象
        //第一个参数 密码原文  第二个是 随机盐  第三个是hash次数
        Md5Hash md5Hash = new Md5Hash(password, salt, 1024);

        String sercetPwd = md5Hash.toHex();

        user.setPassword(sercetPwd);

        save(user);
    }
}
